﻿//********************************************************************************//
//创建日期：<创建日期，2016-12-14>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：预测分析
//********************************************************************************//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Platform.Http;
using System.Text;
using Web.Class;

namespace Web.Pages.YongShui.ZhiBiaoFX
{
    public partial class YuCeFX : BasePage
    {
        #region 属性

        /// <summary>
        /// 保存结果集(计划执行情况)
        /// </summary>
        private DataTable dtJiHuaZXQK
        {
            get
            {
                return ViewState["dtJiHuaZXQK"] as DataTable;
            }
            set
            {
                ViewState["dtJiHuaZXQK"] = value;
            }
        }

        /// <summary>
        /// 保存结果集(特殊用户)
        /// </summary>
        private DataTable dtTeShuYH
        {
            get
            {
                return ViewState["dtTeShuYH"] as DataTable;
            }
            set
            {
                ViewState["dtTeShuYH"] = value;
            }
        }

        /// <summary>
        /// 保存结果集(预测分析)
        /// </summary>
        private DataTable dtYuCheFX
        {
            get
            {
                return ViewState["dtYuCheFX"] as DataTable;
            }
            set
            {
                ViewState["dtYuCheFX"] = value;
            }
        }

        #endregion

        #region 变量

        /// <summary>
        /// 数据访问操作类
        /// </summary>
        SpHelper _spHelper = new SpHelper();

        /// <summary>
        /// 计划参数
        /// </summary>
        YS_JH_YUBIANZJHPZ _YS_JH_YUBIANZJHPZ = new YS_JH_YUBIANZJHPZ();

        /// <summary>
        /// 计划参数BLL
        /// </summary>
        YS_JH_YUBIANZJHPZBLL _YS_JH_YUBIANZJHPZBLL = new YS_JH_YUBIANZJHPZBLL();

        /// <summary>
        /// 特殊用户对象
        /// </summary>
        YS_JH_BUDUIFLSPH _YS_JH_BUDUIFLSPH = new YS_JH_BUDUIFLSPH();

        /// <summary>
        /// 特殊用户BLL
        /// </summary>
        YS_JH_BUDUIFLSPHBLL _YS_JH_BUDUIFLSPHBLL = new YS_JH_BUDUIFLSPHBLL();

        /// <summary>
        /// 操作日志BLL
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitControl();
            }
            //用于执行导出
            this.ScriptManager1.RegisterPostBackControl(this.btnActPerformExport);
        }

        /// <summary>
        /// 绑定空表头
        /// </summary>
        protected override void Render(HtmlTextWriter sender)
        {
            base.Render(sender);
        }

        /// <summary>
        /// 回调响应
        /// </summary>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            CallbackResult cr = new CallbackResult();
            cr.OpType = e.Parameter;
            if (e.Parameter == "tab4export")
            {
                if (this.tab4gvList1.VisibleRowCount == 0)
                {
                    cr.IsAlertMsg = true;
                    cr.Msg = "没有可供导出的数据";
                    e.Result = cr.ToString();
                }
                else
                {
                    cr.IsSuccess = true;
                    e.Result = cr.ToString();
                }
            }
        }

        /// <summary>
        /// 前台验证后实际执行后台操作
        /// </summary>
        protected void btnActPerform_Click(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 前台验证后实际执行后台操作
        /// </summary>
        protected void btnActPerformExport_Click(object sender, EventArgs e)
        {           
            if (this.hdCollection.Get("type").ToString() == "tab4export")
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("部门");
                dt.Columns.Add("计划量");
                dt.Columns.Add("计划增减");
                dt.Columns.Add("实用量");
                dt.Columns.Add("超计划金额");
                dt.Columns.Add("加价户次");
                dt.Columns.Add("加价户数");
                dt.Columns.Add("增加计划户");
                dt.Columns.Add("减少计划户");
                for (int i = 0; i < this.tab4gvList1.VisibleRowCount; i++)
                {
                    DataRow dr = dt.NewRow();
                    dr["部门"] = this.tab4gvList1.GetRowValues(i, "S_ZHANDIAN");
                    dr["计划量"] = this.tab4gvList1.GetRowValues(i, "I_PLAN");
                    dr["计划增减"] = this.tab4gvList1.GetRowValues(i, "I_PLANZJ");
                    dr["实用量"] = this.tab4gvList1.GetRowValues(i, "I_SYSL");
                    dr["超计划金额"] = this.tab4gvList1.GetRowValues(i, "N_CJHJE");
                    dr["加价户次"] = this.tab4gvList1.GetRowValues(i, "I_JJHC");
                    dr["加价户数"] = this.tab4gvList1.GetRowValues(i, "I_JJHS");
                    dr["增加计划户"] = this.tab4gvList1.GetRowValues(i, "I_ZPLANHS");
                    dr["减少计划户"] = this.tab4gvList1.GetRowValues(i, "I_JPLANHS");
                    dt.Rows.Add(dr);
                }
                PageTools.CreateExcel(dt, this.tab4cmbType.Text + "_" + this.tab4txtY.Text + ".xls");
            }
        }


        /// <summary>
        /// 刷新预测分析列表
        /// </summary>
        protected void tab4gvList1_CustomCallback(object sender, DevExpress.Web.ASPxGridViewCustomCallbackEventArgs e)
        {
            LoadDataYuCheFX();
        }

        #endregion

        #region 方法

        /// <summary>
        /// 初始化控件及数据
        /// </summary>
        private void InitControl()
        {
            //获取水量年月
            int ym = Convert.ToInt32(_spHelper.P_YS_GETSETTINGSVALUE(eSETTINGS_KEYNAME.WATER_Y_M.ToString()).Tables[0].Rows[0][0]);
            //在前台记录下水量年用于前台判断输出年月不能超出水量年
            this.hdCollection.Set("ym", ym);
            int y = ym / 100;
            int m = ym % 100;
            //获取计划参数
            DataRowCollection drc = this._YS_JH_YUBIANZJHPZBLL.SelectYS_JH_YUBIANZJHPZsDynamic(string.Format("I_Y={0}", (Convert.ToInt32(ym) / 100)), null).Rows;
            if (drc.Count == 0)
            {
                drc = this._YS_JH_YUBIANZJHPZBLL.SelectYS_JH_YUBIANZJHPZsDynamic(string.Format("I_Y={0}", (Convert.ToInt32(ym) / 100) - 1), null).Rows;
            }
            //记录加价倍率
            this.hdCollection.Set("jjbl", drc.Count == 0 ? "" : drc[0]["S_JIAJIABL"].ToString());
            this.hdCollection.Set("bbxy", drc.Count == 0 ? "" : drc[0]["S_BENBUFN"]);
            this.hdCollection.Set("bscxy", drc.Count == 0 ? "" : drc[0]["S_BANSHICFN"]);
            //加载预测分析
            this.tab4txtY.Text = DateTime.Now.Year.ToString();
            //this.tab4lblBenBuFA.Value = drc[0]["S_BENBUFN"].ToString() == "1" ? "本部选用方案一" : drc[0]["S_BENBUQDFN"].ToString() == "2" ? "本部选用方案二" : drc[0]["S_BENBUQDFN"].ToString() == "3" ? "本部选用方案三" : "无方案";
            //this.tab4lblBanShiCFA.Value = drc[0]["S_BANSHICFN"].ToString() == "1" ? "办事处选用方案一" : drc[0]["S_BANSHICQDFN"].ToString() == "2" ? "办事处选用方案二" : drc[0]["S_BANSHICQDFN"].ToString() == "3" ? "办事处选用方案三" : "无方案";
        }

      
        /// <summary>
        /// 数据加载
        /// </summary>
        private void LoadDataYuCheFX()
        {
            this.tab4gvList1.DataSource = SearchSpHelper.P_YSJH_YUBIANZHIJHFASJTJ(int.Parse(this.tab4txtY.Text), Convert.ToInt32(this.tab4cmbType.SelectedIndex), Convert.ToInt32(this.hdCollection.Get("jjbl")), Convert.ToInt32(this.hdCollection.Get("bbxy")), Convert.ToInt32(this.hdCollection.Get("bscxy"))).Tables[0];
            this.tab4gvList1.DataBind();
        }

        

        #endregion
    }
}